/*  main.cpp
    
    See README for installation and execution procedure.

    Nicholas Green
    Environment
 */
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "lex.h"
#include "lexeme.h"
#include "types.h"
#include "f_grammar.h"
#include "evaluator.h"

int main(int argc, char* argv[])
{
  FILE* fp;
  const char* file;
  const char* arg;
  bool eval = false;
  bool parse = false;
  int x = 3;
  if(argc < 3)
  {
    printf("Not enough arguments. dpl <source.file> -argument.\n");
    printf("Arguments: P - parser, E - evaluator");
    exit(1);
  }
  else
  {
    file = argv[1];
    arg = argv[2];
    if( (strcmp(arg, "-P") == 0) || (strcmp(arg, "-p") == 0) )
    {
      eval = false;
      parse = true;
    }
    else if( (strcmp(arg, "-R") == 0) || (strcmp(arg, "-r") == 0) )
    {
      eval = true;
      parse = true;
    }
    else
    {
      printf("Incorrect arguments.\n");
      printf("Arguments: P - parser, E - evaluator");
    }
  }

  fp = fopen(file,"r");

  if(fp == NULL)
  {
    printf("File does not exist.");
    exit(1);
  }

  

  Evaluator evaluator(fp);
  if(parse && !eval)
  {
    printf("Parsing complete. Print tree? y/N\n");
    char ch = 'Y';
    scanf("%c",&ch);

    if(ch == 'Y' || ch == 'y')
    {
      evaluator.printParseTree();
    }

    printf("Pretty print? y/N\n");
    char ch1 = 'Y';
    getchar();
    scanf("%c", &ch1);
    
    if(ch1 == 'Y' || ch1 == 'y')
    {
      evaluator.prettyPrint();
    }
  }
  if(eval)
    evaluator.start();



#ifdef _WIN32
  //Hold the console on the screen
  //so you can see the output.
  system("pause");
#endif
  return 0;
}

